package com.kuang.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * Author: Zhang Dongwei
 * Date: 2022/7/29 20:06
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//    授权
//    链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        首页所有人可以访问，功能页只有对应有权限的人才能访问
//        请求授权的规则
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

//        没有权限默认到登录页面,需要开启登录的页面
//      login
        http.formLogin();
//        注销, 开启了注销功能
        http.logout().logoutSuccessUrl("/");

//        防止网站工具：get，post
        http.csrf().disable(); // 关闭scrf功能，关闭登录报错时加

    }

//    认证
//    密码编码：PasswordEncoder
//    在spring security 5.0+ 新增了许多的加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        这些数据正常应该从数据库中读取
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("zhangdongwei").password(new BCryptPasswordEncoder().encode("111111")).roles("vip2", "vip3")
                .and().withUser("root").password(new BCryptPasswordEncoder().encode("111111")).roles("vip1","vip2", "vip3")
                .and().withUser("guest").password(new BCryptPasswordEncoder().encode("111111")).roles("vip1");
    }
}
